package wqh.controller.file;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import wqh.domain.GeoBuilding;
import wqh.pojo.MapRet;
import wqh.service.GeoBuildingService;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by devts on 2016/11/14.
 */
@Controller
@CrossOrigin(origins = "*")
@RequestMapping(value = "/xkGeoLocationMap")
public class GeoBuildingMapPicture {

    @Autowired
    private GeoBuildingService geoBuildingService;

    @ResponseBody
    @RequestMapping(value = "/mapDownload",method = RequestMethod.GET)
    public ResponseEntity<byte[]> mapDownload(HttpServletRequest httpServletRequest, @RequestParam(value = "id",required = true) int id) throws IOException
    {
     try {
         GeoBuilding geoBuilding = geoBuildingService.findRecordById(id);
         String localPath = null;
         if(geoBuilding != null) localPath = geoBuilding.getPicture();
         String serverPath = httpServletRequest.getServletContext().getRealPath("/") + localPath;
         File file = new File(serverPath);
         if(!file.exists()) file = new File(httpServletRequest.getServletContext().getRealPath("/") + "\\WEB-INF\\ftl\\assets\\img\\error.jpg");
         HttpHeaders httpHeaders = new HttpHeaders();
         String fileName = file.getName();
         httpHeaders.setContentDispositionFormData("attachment",java.net.URLEncoder.encode(fileName,"UTF-8"));
         httpHeaders.setContentType(MediaType.IMAGE_JPEG);
         return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.CREATED);
     }catch (Exception e){
         e.printStackTrace();
         return null;
     }
    }

    @ResponseBody
    @RequestMapping(value = "/mapUpload",method = RequestMethod.POST)
    public Object mapUpload(HttpServletRequest httpServletRequest,
                            @RequestParam(value = "file",required = true)MultipartFile file,
                            @RequestParam(value = "id",required = true) int id)
    {
        try{
            GeoBuilding g = geoBuildingService.findRecordById(id);
            if(g == null)
            {
                return MapRet.SQL_SELECT_NONE();
            }
            String localPath = "WEB-INF\\upload\\map\\";
            String uploadPath = httpServletRequest.getServletContext().getRealPath("/") + localPath;
            if(g.getPicture() != null && g.getPicture().length() != 0 && !g.getPicture().equals("0"))
            {
                String serverPath = httpServletRequest.getServletContext().getRealPath("/") + g.getPicture();
                File serverFile = new File(serverPath);
                if(serverFile.isFile() && serverFile.exists())
                {
                    serverFile.delete();
                }
            }
            File uploadDir = new File(uploadPath);
            if(!uploadDir.exists())
            {
                uploadDir.mkdirs();
            }
            String fileName = file.getOriginalFilename();
            if(fileName != null)
            {
                byte[] bytes = file.getBytes();
                fileName = System.currentTimeMillis() + fileName;
                File serverFile = new File(uploadDir.getAbsolutePath() + File.separator + fileName);
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
                stream.write(bytes);
                stream.close();
                geoBuildingService.updateMap(localPath + serverFile.getName(), id);
            }

        }catch (Exception e){
            e.printStackTrace();
            return MapRet.FILE_UPLOAD_ERROR();
        }
        return MapRet.FILE_UPLOAD_SUCCESS();
    }

}
